package com.neuedu.corejava.ch12;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TestStudent {
	private static final String FILE_NAME = "c:/student.data";

	// 编写一段代码，从键盘输入3个学生的信息，并保存到文件中
	public static void inputStudentInfo() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入3个学生的信息：");

		List<Student> list = new ArrayList<>();
		Student student = null;
		for (int i = 0; i < 3; i++) {
			int id = 0;
			try {
				System.out.println("请输入学号（数字）：");
				String idString = scanner.nextLine();
				id = Integer.parseInt(idString);
				
			} catch (NumberFormatException e) {
				
			}
			System.out.println("请输入姓名：");
			String name = scanner.nextLine();

			int age = 0;
			try {
				System.out.println("请输入年龄（数字）：");
				String ageString = scanner.nextLine();
				age = Integer.parseInt(ageString);
			} catch (Exception e) {
			}
			System.out.println("请输入性别：");
			String sex = scanner.nextLine();
			student = new Student(id, name, age, sex);
			list.add(student);
		}
		File file = new File(FILE_NAME);
		FileOutputStream outputStream = null;
		ObjectOutputStream objectOutputStream = null;
		try {
			if (!file.exists()) {
				file.getParentFile().mkdirs();
				file.createNewFile();
			}
			// 操作文件-节点流
			outputStream = new FileOutputStream(file,true);
			// 对流进行包装转换，处理流
			objectOutputStream = new ObjectOutputStream(outputStream);
			// 使用对象流把一个java.lang.Object类的对象输出到一个流
			objectOutputStream.writeObject(list);

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (objectOutputStream != null) {
				try {
					objectOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (outputStream != null) {
					try {
						outputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
	public static void readDataFromFile() {
		File file=new File(FILE_NAME);
		if(!file.exists()) {
			System.out.println("找不到文件："+FILE_NAME);
			return;
		}
		FileInputStream inputStream=null;
		ObjectInputStream objectInputStream=null;
		try {
			inputStream=new FileInputStream(file);
			objectInputStream=new ObjectInputStream(inputStream);
			try {
				Object object=objectInputStream.readObject();
				List<Student>list=(List<Student>) object;//向下溯型
				for (Student student : list) {
					System.out.println(student);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
		}finally {
			if(objectInputStream!=null) {
				try {
					objectInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
				if(inputStream!=null) {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	public static void main(String[] args) {

		// 编写一段代码，从键盘输入3个学生的信息，并保存到文件中
		//inputStudentInfo();// 序列化对象到文件中

		// 编写一段代码，从文件中读取数据，并还原为一组学生信息
		readDataFromFile();
	}

}
